import domMessage from '../utils/messageOnce'
import {
  getDictionary,
} from "@/api/baseconfig/dictionary";
const messageOnce = new domMessage()
import axios from 'axios'

let mixin = {
  data() {
    return {}
  },
  methods: {
    cloneObj(obj) {
      return JSON.parse(JSON.stringify(obj))
    },
    //获取字典信息
    getDic(info) {
      let query = {
        pageNum: 1,
        pageSize: 100,
        info,
      }
      return getDictionary(query)
    },
    download(url) {
      const a = document.createElement('a')
      a.setAttribute('download', '')
      a.setAttribute('href', url)
      a.click()
    },
    postDownload(downloadUrl, data,method='post') {
      return new Promise((resolve,reject)=>{
        axios(
          {
            method,
            url: downloadUrl,
            data,
            responseType: 'blob'    // 指定获取数据的类型为blob
          }
        ).then(
          function(response) {
            console.log(response)
            // 前提是服务端要在header设置Access-Control-Expose-Headers: Content-Disposition
            // 前端才能正常获取到Content-Disposition内容
            const disposition = response.headers['content-disposition']
            let fileName = disposition.substring(disposition.indexOf('filename=') + 9, disposition.length)
            // iso8859-1的字符转换成中文
            fileName = decodeURIComponent(fileName)
            // 去掉双引号
            fileName = fileName.replace(/\"/g, '')
            const content = response.data
            console.info('rep:', disposition)
            console.info('fileName:', fileName)
            // 创建a标签并点击， 即触发下载
            let url = window.URL.createObjectURL(new Blob([content]))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName)
            //link.download = "测试下载文件.xls"
            // 模拟
            document.body.appendChild(link)
            link.click()
            // 释放URL 对象
            window.URL.revokeObjectURL(link.href)
            document.body.removeChild(link)
            resolve()
          }
        ).catch(function(err) {
          console.log(err)
          reject()
        })
      })
    },
    confirm(message = '此操作将永久删除, 是否继续?') {
      return this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
    },
    resetConfirm(message = '此操作将重置密码为与账号一致, 是否继续?') {
      return this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
    },
    handleModform(row, form) {
      Object.keys(form).forEach(key => {
        form[key] = row[key]
      })
    },
    tip(message = '请先勾选表格中的一条数据') {
      this.$message.warning(message)
    },
    //将数组为空的设为undefined
    getGoodsTypeTreeData(data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].childrenTypes.length < 1) {
          // children若为空数组，将children设为undefined
          delete data[i].childrenTypes
        } else {
          // children若不为空数组，继续递归调用
          this.getGoodsTypeTreeData(data[i].childrenTypes)
        }
      }
      return data
    },
    emptyStoreTip() {
      messageOnce.error({
        message: '请先在头部导航处选择一个门店！',
        duration: 5 * 1000
      })
    },
    //自动精确到小数后两位
    oninput(value, isEmpty) {
      if (!value) {
        if (isEmpty) {
          return ''
        } else {
          return '0.00'
        }
      }
      var value = Math.round(parseFloat(value) * 100) / 100
      var xsd = value.toString().split('.')
      if (xsd.length == 1) {
        value = value.toString() + '.00'
        return value
      }
      if (xsd.length > 1) {
        if (xsd[1].length < 2) {
          value = value.toString() + '0'
        }
        return value
      }
    },
    handleDate(date,startName='startDate',endName='endDate'){
      let obj = {}
      if(date){
        obj[startName] = date[0] ? date[0] + ' 00:00:00' : ''
        obj[endName] = date[1] ? date[1] + ' 23:59:59' : ''
      }
      return obj
    }
  }
};
export default mixin;
